package logic

import (
	"context"
	"fmt"
	"person/models/mysql"
	"person/personrpc/internal/svc"
	"person/personrpc/personrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateLogic) Update(in *personrpc.UpdateRequest) (*personrpc.UpdateResponse, error) {
	// todo: add your logic here and delete this line

	var update mysql.Account
	mysql.DB.Debug().Model(&update).Where("tel = ?", in.Tel).Find(&update)

	if update.ID == 0 {
		return nil, fmt.Errorf("账户不存在")
	}
	update.Password = in.Password
	err := mysql.DB.Debug().Model(&update).Updates(&update).Error
	if err != nil {
		return nil, fmt.Errorf("修改失败")
	}
	return &personrpc.UpdateResponse{Id: int64(update.ID)}, nil
}
